package com.hbnu.controller;

import com.hbnu.domain.User;
import com.hbnu.service.UserService;
import com.hbnu.utils.CurrentHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

/**
 * @version: 1.0
 * @ClassName: Login
 * @Description: TODO
 * @Author: 86132
 * @Date: 2025/11/5 9:00
 */
@Component
public class Login {
    private static final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    //登录
    public boolean login(User user) {
        //获取用户名和密码
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名：");
        user.setUsername(sc.next());
        System.out.print("请输入密码：");
        user.setPassword(sc.next());
        //查询用户表数据
        UserService userService = (UserService) applicationContext.getBean("userService");
        List<User> userList = userService.findAll();
        //判断用户输入的账户密码是否存在
        for (User u : userList) {
            if (u.getUsername().equals(user.getUsername()) && u.getPassword().equals(user.getPassword())) {
                // 将用户id存入线程中
                CurrentHolder.setCurrentId(u.getId());
                //传递用户的id和手机号
                user.setId(u.getId());
                user.setPhone(u.getPhone());
                return true;
            }
        }
        return false;
    }

}
